package com.moleq.dialog;

import java.util.List;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.mgpos.activities.R;
import com.moleq.models.RoundingSetting;

public class RoundEditDialog extends Dialog implements OnResultListener {

	private OnResultListener mOnResultListener;
	private Button btn_round_ok,btn_round_cancel;
	private CheckBox cb_option;
	private EditText et_roundno, et_roundname, et_downvalue, et_upvalue, et_tovalue;
	private LinearLayout ll_dialog_perview_mode_layout;
	private Context mContext;
	private List<RoundingSetting> listRound;
	private RoundingSetting mRoundingSetting;
	private MessageDialog mMessageDialog;
	private String action = "";
	double downvalue = 0, upvalue = 0, tovalue = 0;
	
	public void onCloseWithReturn(int what,Object obj){
		mOnResultListener.onReturnValue(what,obj);
		dismiss();
	}

	public RoundEditDialog(Context context, Object base) {
		super(context,R.style.dialog2);
		constructor(context,base);
	}
	
	public void setSource(List<RoundingSetting> listPrinter,RoundingSetting round,String action){
		this.listRound = listPrinter;
		this.mRoundingSetting = round;
		this.action = action;
	}
	private void constructor(Context context,Object base) {
		mContext = context;
		mOnResultListener = (OnResultListener) base;
	}

	public RoundEditDialog(Context context, int theme) {
		super(context, theme);
		constructor(context);
	}
	
	private void constructor(Context context) {
		// TODO Auto-generated method stub
		mContext = context;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.ps_dialog_edit_round);
		this.setCancelable(false);
	
		ll_dialog_perview_mode_layout = (LinearLayout) findViewById(R.id.ll_dialog_perview_mode_layout);
		et_roundno = (EditText) findViewById(R.id.et_roundno);
		et_roundname = (EditText) findViewById(R.id.et_roundname);
		et_downvalue = (EditText) findViewById(R.id.et_downvalue);
		et_upvalue = (EditText) findViewById(R.id.et_upvalue);
		et_tovalue = (EditText) findViewById(R.id.et_tovalue);
		cb_option = (CheckBox) findViewById(R.id.cb_option);
		
		btn_round_ok = (Button) findViewById(R.id.btn_round_ok);
		btn_round_cancel = (Button) findViewById(R.id.btn_round_cancel);
		ll_dialog_perview_mode_layout.setOnClickListener(OnClickDialogWindowListener);
		btn_round_ok.setOnClickListener(OnClickDialogWindowListener);
		btn_round_cancel.setOnClickListener(OnClickDialogWindowListener);
	}
	
	public void show(){
		super.show();	
		getDetail(mRoundingSetting);
		et_roundname.requestFocus();
	}
	
	public void getDetail(RoundingSetting round){
//		et_roundno.setText(String.valueOf(round.getRs_id()));
		et_roundno.setText(String.valueOf(round.getNo()));
		et_roundname.setText(round.getRs_name());
		double downvalue = round.getRs_down_value();
		if (downvalue != 0) {
			et_downvalue.setText(String.format("%.2f",downvalue));
		}else {
			et_downvalue.setText("");
		}
		
		double upvalue = round.getRs_up_value();
		if (upvalue != 0) {
			et_upvalue.setText(String.format("%.2f",upvalue));
		}else {
			et_upvalue.setText("");
		}
		
		double tovalue = round.getRs_to_value();
		if (tovalue != 0) {
			et_tovalue.setText(String.format("%.2f",tovalue));
		}else {
			et_tovalue.setText("");
		}
		if (round.isRs_optional()) {
			cb_option.setChecked(true);
		}else {
			cb_option.setChecked(false);
		}
	}
	
	public Button.OnClickListener OnClickDialogWindowListener = new Button.OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_round_ok:
				if (validation() == false) return;
				
				mRoundingSetting.setRs_name(et_roundname.getText().toString().trim());
				mRoundingSetting.setRs_down_value(downvalue);
				mRoundingSetting.setRs_up_value(upvalue);
				mRoundingSetting.setRs_to_value(tovalue);
				if (cb_option.isChecked()) {
					mRoundingSetting.setRs_optional("1");
				}else {
					mRoundingSetting.setRs_optional("0");
				}
				if (mRoundingSetting.getType().equals("I") && action.equals("add")) {
					listRound.add(mRoundingSetting);
				}
				mRoundingSetting.setEdit(true);
				
				onCloseWithReturn(R.id.btn_round_ok, listRound);
				dismiss();
				break;
			case R.id.btn_round_cancel:
				dismiss();
				break;
			case R.id.ll_dialog_perview_mode_layout:
				InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
				inputMethodManager.hideSoftInputFromWindow(RoundEditDialog.this
						.getCurrentFocus().getWindowToken(),
						InputMethodManager.HIDE_NOT_ALWAYS);
				break;
			}
		}
	};
	
	public boolean validation(){
		boolean bo = true;
		
		if (!et_downvalue.getText().toString().trim().equals("")) {
			downvalue = Double.valueOf(et_downvalue.getText().toString().trim());
		}
		if (!et_upvalue.getText().toString().trim().equals("")) {
			upvalue = Double.valueOf(et_upvalue.getText().toString().trim());
		}
		if (!et_tovalue.getText().toString().trim().equals("")) {
			tovalue = Double.valueOf(et_tovalue.getText().toString().trim());
		}
		
		if (et_roundname.getText().toString().trim().equals("")) {
			ShowMessage("Name can not be empty.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			et_roundname.requestFocus();
			bo = false;
		}else if (et_downvalue.getText().toString().trim().equals("")) {
			ShowMessage("Down value can not be empty.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			et_downvalue.requestFocus();
			bo = false;
		}else if (et_upvalue.getText().toString().trim().equals("")) {
			ShowMessage("Up value can not be empty.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			et_upvalue.requestFocus();
			bo = false;
		}else if (et_tovalue.getText().toString().trim().equals("")) {
			ShowMessage("To value can not be empty.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			et_tovalue.requestFocus();
			bo = false;
		}else if (downvalue >= upvalue && !et_upvalue.getText().toString().trim().equals("")) {
			ShowMessage("Invalid values.\nDown value must be lower than Up value,and Up value must be lower than To value.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			et_downvalue.requestFocus();
			bo = false;
		}else if (downvalue > tovalue) {
			ShowMessage("Invalid values.\nDown value must be lower than Up value,and Up value must be lower than To value.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			et_downvalue.requestFocus();
			bo = false;
		}else if (upvalue > tovalue) {
			ShowMessage("Invalid values.\nDown value must be lower than Up value,and Up value must be lower than To value.",
					mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			et_upvalue.requestFocus();
			bo = false;
		}
		return bo;
	}
	
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener(){

		public void onClickYes() {
			
		}

		public void onClickNo() {
			mMessageDialog.dismiss();
		}

		public void onClickOK() {
			mMessageDialog.dismiss();
		}
	};
	
	public void ShowMessage(String text,int type,int icon){
		mMessageDialog = new MessageDialog(mContext);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}

	@Override
	public void onCloseWithReturn(Class<? extends MoleQBaseDialog> pramClass,
			int viewId, String returnvalue) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onReturnValue(int what, Object obj) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onShowingMessageDlg(boolean isShowing) {
		// TODO Auto-generated method stub
		
	}
}
